package com.bosicloud.cmb.ibp.account.component;

import com.bosicloud.cmb.ibp.account.common.model.IbpUserApiKey;
import com.bosicloud.cmb.ibp.account.service.AccountManagerService;
import com.bosicloud.cmb.ibp.aws.adapter.commom.vo.ApiKeyVO;
import com.bosicloud.cmb.ibp.commom.dto.exchange.ExchangeMessage;
import com.bosicloud.cmb.ibp.commom.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.bosicloud.cmb.ibp.commom.dto.exchange.ExchangeAction.CREATE_KEY;

/**
 * @author Jack Peng
 */
@Component
@Slf4j
public class MessageExecutor {

    @Autowired
    private AccountManagerService accountManagerService;

    public void receiveLogic(ExchangeMessage payload) {
        switch (payload.getAction()) {
            case CREATE_KEY:
                createApiKey(payload);
                break;
            default:
                break;

        }
    }

    private void createApiKey(ExchangeMessage payload) {
        ApiKeyVO apiKey = JsonUtil.parse(JsonUtil.toJson(payload.getData()), ApiKeyVO.class);
        IbpUserApiKey ibpUserApiKey = IbpUserApiKey.builder()
                .awsKey(apiKey.getApiKey())
                .awsKeyId(apiKey.getKeyId())
                .userId(apiKey.getUserId())
                .env(apiKey.getEnv())
                .build();
        accountManagerService.createUserApiKey(ibpUserApiKey);
    }
}
